package com.ccdt.tv.roundprogram.round;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ccdt.tv.roundprogram.Constant;
import com.ccdt.tv.roundprogram.bean.ChannelRoot;
import com.ccdt.tv.roundprogram.bean.PlayInfo;
import com.ccdt.tv.roundprogram.media.IjkVideoView;

import rx.Subscription;
import rx.functions.Action1;

/**
 * create by hfq 2018/10/15
 * 实现类
 */
public class RoundPresenter implements RoundContract.Presenter {

    private final String TAG = "RoundPresenter";
    String TEST_PLAY_URL = "http://10.177.16.81:8040/live?device_id=10001&bitrate=400";

    private static final int DURATION_TO_HIDE = 5000;

    private RoundContract.View mView;
    private RoundContract.Model mModel;
    private ChannelRoot mChannelRoot;

    private Subscription mSubscription;

    private MyHandler myHandler = new MyHandler();

    public RoundPresenter(RoundContract.View view) {
        this.mView = view;
        this.mModel = new RoundModel();
    }

    /**
     * 通过model拿数据，通知给view层
     */
    @Override
    public void loadChannel() {
        String client = "8230110374087890";//TODO 暂时写死
        mSubscription = mModel.getChannel(client).subscribe(new Action1<ChannelRoot>() {
            @Override
            public void call(ChannelRoot channelRoot) {
                mChannelRoot = channelRoot;
//                mChannelRoot = ChannelRoot.makeDemoData();

                if (mChannelRoot != null && mChannelRoot.getChannel() != null
                        && mChannelRoot.getChannel().size() > 0) {
                    //TODO 暂时默认播第一个节目
                    mView.setChannel(mChannelRoot);
                    turnOverProgram(0, true);
                    sendHideMsg();
                } else {
                    LogUtils.eTag(TAG, "mChannelRoot is null");
                }
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
                ToastUtils.showShort("Get channel ERROR");
            }
        });
    }

    @Override
    public void turnOverProgram(int position, boolean isValid) {
        if (isValid) {
            mSubscription = mModel.getPlayInfo(position, mChannelRoot).subscribe(new Action1<PlayInfo>() {
                @Override
                public void call(PlayInfo playInfo) {
                    play(playInfo.getPlayUrl());
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    throwable.printStackTrace();
                    ToastUtils.showShort("Get Play url ERROR");
                }
            });
        }
    }

    private void play(String url) {
//        ToastUtils.showLong("URL ： " + url);
        IjkVideoView videoView = mView.getVideoView();
        if (videoView.isPlaying()) {
            videoView.stopPlayback();
            videoView.release(true);
        }
        videoView.setVideoPath(url);
        videoView.start();
    }

    @Override
    public void onViewDestroy() {
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
        mView.getVideoView().stopPlayback();
        mView.getVideoView().release(true);
    }

    @Override
    public void sendHideMsg() {
        myHandler.removeCallbacks(runnableHide);
        myHandler.postDelayed(runnableHide, DURATION_TO_HIDE);
    }

    private Runnable runnableHide = new Runnable() {
        @Override
        public void run() {
            mView.hideChannelView();
        }
    };

    @SuppressLint("HandlerLeak")
    class MyHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    }

}
